﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

/// <summary>
///JsonHelper 的摘要说明
/// </summary>
namespace VCommons
{
    public static class JsonHelper
    {
        private static JsonSerializerSettings _jsonSettings;

        static JsonHelper()
        {
            IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverter();
            datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            _jsonSettings = new JsonSerializerSettings();
            _jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
            _jsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            _jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            _jsonSettings.Converters.Add(datetimeConverter);
        }


        /// <summary>
        /// 将指定的对象序列化成 JSON 数据。
        /// </summary>
        /// <param name="obj">要序列化的对象。</param>
        /// <returns></returns>  
        public static string ToJson(this object obj)
        {
            try
            {
                if (null == obj)
                    return null;

                if (obj.GetType() == typeof(System.Data.DataTable))
                {
                    System.Data.DataTable _sourceTable = (System.Data.DataTable)obj;
                    foreach (System.Data.DataRow row in _sourceTable.Rows)
                    {
                        foreach (System.Data.DataColumn column in _sourceTable.Columns)
                        {
                            if (column.DataType == typeof(System.Int32))
                            {
                                if (row[column] == DBNull.Value)
                                {

                                    row[column.ColumnName] = 0;
                                }
                            }
                            if (column.DataType == typeof(System.Decimal))
                            {

                                if (row[column] == DBNull.Value)
                                {
                                    row[column.ColumnName] = 0M;
                                }

                            }

                        }
                    }
                    _sourceTable.AcceptChanges();
                    return JsonConvert.SerializeObject(_sourceTable, Formatting.None, _jsonSettings);

                }
                else
                {
                    return JsonConvert.SerializeObject(obj, Formatting.None, _jsonSettings);
                }


            }
            catch (Exception ex)
            {
                return null;

            }


        }

        /// <summary>
        /// 将指定的 JSON 数据反序列化成指定对象。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="json">JSON 数据。</param>
        /// <returns></returns>
        public static T FromJson<T>(this string json)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(json, _jsonSettings);
            }
            catch (Exception ex)
            {

                return default(T);
            }
        }
    }
}